package top.hundreds.oasys.controller.FrontController;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import top.hundreds.common.crud.bean.PageParam;
import top.hundreds.common.utils.JJwtUtil;
import top.hundreds.common.web.annotation.MultiRequestBody;
import top.hundreds.common.web.api.ApiResult;
import top.hundreds.oasys.bean.constant.SystemConst;
import top.hundreds.oasys.bean.entity.User;
import top.hundreds.oasys.bean.vo.UserVo;
import top.hundreds.oasys.service.UserService;
import top.hundreds.oasys.utils.Md5Util;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/front/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 登录
     *
     * @param user 账号密码
     * @return token/fail
     */
    @PostMapping("/login")
    public ApiResult<String> login(@MultiRequestBody User user) {
        System.out.println(user);
        User one = userService.getOne(User.builder().username(user.getUsername()).build());
        if (one == null) {
            return ApiResult.fail("账号密码错误");
        }
        String password = Md5Util.generate(user.getPassword(), one.getSalt());
        if (password.equals(one.getPassword())) {
            String token = JJwtUtil.createJWT(one.getId().toString(), null, SystemConst.USER_JWT_KEY);
            return ApiResult.success(token);
        }
        return ApiResult.fail("账号密码错误");
    }

    /**
     * info
     *
     * @param id 调用者id
     * @return UserVo
     */
    @GetMapping("/info")
    public ApiResult<UserVo> info(@RequestAttribute("id") Long id) {
        User one = this.userService.get(id);
        UserVo userVo = this.userService.buildVo(one);
        BeanUtil.copyProperties(one, userVo);
        return ApiResult.success(userVo);
    }

    /**
     * 注册
     *
     * @param user 用户类
     * @return success
     */
    @PostMapping("/register")
    public ApiResult<String> register(@MultiRequestBody User user) {
        if (user.getUsername() == null || user.getPassword() == null) {
            return ApiResult.fail();
        }
        if (userService.getOne(User.builder().username(user.getUsername()).build()) != null) {
            return ApiResult.fail("账号已存在");
        }
        user.setSalt(Md5Util.getSalt());
        user.setPassword(Md5Util.generate(user.getPassword(), user.getSalt()));
        if (userService.insert(user) != null) {
            return ApiResult.success();
        } else {
            return ApiResult.fail();
        }
    }

    /**
     * 编辑用户
     *
     * @param user 用户类
     * @return success
     */
    @PutMapping("/save")
    public ApiResult<String> save(@RequestBody User user) {
        if (user.getId() != null) {
            this.userService.update(user);
        } else {
            user.setSalt(Md5Util.getSalt());
            String md5Pwd = Md5Util.generate(user.getPassword(),user.getSalt());
            user.setPassword(md5Pwd);
            this.userService.insert(user);
        }
        return ApiResult.success();
    }

    /**
     * 修改密码
     *
     * @param oldPwd     旧密码
     * @param newPwd     新密码
     * @param confirmPwd 确认密码
     * @param id         用户id
     * @return success
     */
    @PutMapping("/password")
    public ApiResult<String> updatePwd(@MultiRequestBody String oldPwd,
                                       @MultiRequestBody String newPwd,
                                       @MultiRequestBody String confirmPwd,
                                       @RequestAttribute("id") Long id) {
        if (StrUtil.isEmpty(oldPwd) || StrUtil.isEmpty(newPwd) || StrUtil.isEmpty(confirmPwd)) {
            return ApiResult.fail("参数为空");
        }
        if (!newPwd.equals(confirmPwd)) {
            return ApiResult.fail("新密码不一致");
        }
        User one = userService.get(id);
        if (Md5Util.generate(oldPwd, one.getSalt()).equals(one.getPassword())) {
            one.setSalt(Md5Util.getSalt());
            one.setPassword(Md5Util.generate(newPwd, one.getSalt()));
            userService.update(one);
            return ApiResult.success();
        }
        return ApiResult.fail("旧密码错误");
    }

    /**
     * 获取列表
     *
     * @param pageParam 分页参数
     * @return 分页
     */
    @PostMapping("/list")
    public ApiResult<List<UserVo>> list(@RequestBody PageParam<User> pageParam) {
        final Page<User> page = this.userService.getPage(pageParam);
        List<User> userList = page.getRecords();
        ArrayList<UserVo> userVoList = new ArrayList<>(userList.size());
        for (User user : userList) {
            userVoList.add(this.userService.buildVo(user));
        }
        return ApiResult.success(userVoList);
    }

    @PostMapping("allocation")
    public ApiResult<String> allocation(@RequestBody User user) {
        this.userService.update(user);
        return ApiResult.success();
    }

    @DeleteMapping("remove/{id}")
    public ApiResult<String> remove(@PathVariable Long id){
        this.userService.delete(id);
        return ApiResult.success();
    }

    @GetMapping("setStatus/{id}/{status}")
    public ApiResult<String> setStatus(@PathVariable Long id, @PathVariable Integer status) {
        this.userService.update(User.builder().id(id).status(status).build());
        return ApiResult.success();
    }

}
